<?php

namespace app\admin\controller\warehouse;

use app\common\controller\Backend;
use Think\Db;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Stock extends Backend
{
    
    /**
     * Stock模型对象
     * @var \app\admin\model\warehouse\Stock
     */
    protected $model = null;
    protected $searchFields = 'id,product_bn';
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\warehouse\Stock;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['products'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['products'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                
                
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /*
     * 查看详情
     * */
    public function details($ids=''){
        //取出所有入库出库记录
        $where1['stockitems.id'] = $ids;
        $stockitems_model = new \app\admin\model\warehouse\Stockitems;
        $res = $stockitems_model
            ->with(['products'])
            ->where($where1)
            ->find();
        $res = $res->toArray();
        $final_res = array();
        if(!empty($res)){
            if($this->request->request('batch')){//批次号
                $where['storage.batch'] = $this->request->request('batch');
                $where2['outbound.batch'] = $this->request->request('batch');
            }

            if($this->request->request('warehouse')){//仓库
                $where['warehouse.name'] = $this->request->request('warehouse');
                $where2['warehouse.name'] = $this->request->request('warehouse');
            }
            if($this->request->request('location')){//库位
                $where['location.name'] = $this->request->request('location');
                $where2['location.name'] = $this->request->request('location');
            }
            $where['storage.product_bn'] = $res['product_bn'];
            $storage_model = new \app\admin\model\warehouse\Storage;
            $storage = $storage_model
                    ->with(['warehouse','location'])
                    ->where($where)
                    ->select();
            $outbound_model = new \app\admin\model\warehouse\Outbound;
            $where2['outbound.product_bn'] = $res['product_bn'];
            $out = $outbound_model
                    ->with(['warehouse','location'])
                    ->where($where2)
                    ->select();
            $storage = collection($storage)->toArray();
            foreach($storage as $key => $val){
                $storage[$key]['type'] = '入库';
            }
            $out = collection($out)->toArray();
            foreach($out as $key => $val){
                $out[$key]['type'] = '出库';
            }
            $result = array_merge($storage,$out);
            foreach($result as $key => $val){
                if($this->request->request('type') == '入库'){//类型
                    if(!$val['deletetime'] && $val['type'] == '入库'){
                        $final_res[$key]['product_bn'] = $res['product_bn'];
                        $final_res[$key]['name'] = $res['products']['name'];
                        $final_res[$key]['batch'] = $val['batch'];
                        $final_res[$key]['spec'] = $res['products']['spec'];
                        $final_res[$key]['unit'] = $res['products']['unit'];
                        $final_res[$key]['num'] = $val['num'];
                        $final_res[$key]['warehouse_name'] = $val['warehouse']['name'];
                        $final_res[$key]['location_name'] = $val['location']['name'];
                        $final_res[$key]['type'] = $val['type'];
                        $final_res[$key]['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
                    }
                }elseif($this->request->request('type') == '出库'){
                    if(!$val['deletetime'] && $val['type'] == '出库'){
                        $final_res[$key]['product_bn'] = $res['product_bn'];
                        $final_res[$key]['name'] = $res['products']['name'];
                        $final_res[$key]['batch'] = $val['batch'];
                        $final_res[$key]['spec'] = $res['products']['spec'];
                        $final_res[$key]['unit'] = $res['products']['unit'];
                        $final_res[$key]['num'] = $val['num'];
                        $final_res[$key]['warehouse_name'] = $val['warehouse']['name'];
                        $final_res[$key]['location_name'] = $val['location']['name'];
                        $final_res[$key]['type'] = $val['type'];
                        $final_res[$key]['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
                    }
                }else{
                    if(!$val['deletetime']){
                        $final_res[$key]['product_bn'] = $res['product_bn'];
                        $final_res[$key]['name'] = $res['products']['name'];
                        $final_res[$key]['batch'] = $val['batch'];
                        $final_res[$key]['spec'] = $res['products']['spec'];
                        $final_res[$key]['unit'] = $res['products']['unit'];
                        $final_res[$key]['num'] = $val['num'];
                        $final_res[$key]['warehouse_name'] = $val['warehouse']['name'];
                        $final_res[$key]['location_name'] = $val['location']['name'];
                        $final_res[$key]['type'] = $val['type'];
                        $final_res[$key]['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
                    }
                }
            }
        }
        $datetime = array();
        $final_res = array_values($final_res);
        foreach ($final_res as $user) {
            $datetime[] = $user['createtime'];
        }
        array_multisort($datetime,SORT_DESC,$final_res);
        $this->assign('rows',$final_res);
        $this->assign('ids',$ids);
        $this->assign('batch',$this->request->request('batch'));
        $this->assign('warehouse',$this->request->request('warehouse'));
        $this->assign('location',$this->request->request('location'));
        $this->assign('type',$this->request->request('type'));
        return $this->view->fetch();
    }
}
